﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace libAsymmetricEncryption
{
    public class RSA_Algorithm
    {
        public static byte[] rsaEncryptWithPublicKey(RSA_Key publicKey, byte[] dataToEncrypt)
        {
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
            
            RSAParameters para = new RSAParameters();
            
            para.Modulus = publicKey.get_modulus();
            para.Exponent = publicKey.get_exponent();

            provider.ImportParameters(para);

            byte[] encryptedData = provider.Encrypt(dataToEncrypt, true);

            return encryptedData;
        }

        public static byte[] rsaDecryptWithPrivateKey(RSAParameters privateParameters, byte[] encryptedData)
        {
            RSACryptoServiceProvider provider = new RSACryptoServiceProvider();

            provider.ImportParameters(privateParameters);

            byte[] decryptedData = provider.Decrypt(encryptedData, true);

            return decryptedData;
        }
    }
}
